//
//  DBJIllnessSecondView.m
//  DBJ
//
//  Created by Captain_czq on 16/11/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJIllnessSecondView.h"
#import <Masonry.h>
#import "DBJSecondTabCell.h"


@interface DBJIllnessSecondView ()<UITableViewDelegate,UITableViewDataSource>


@end

@implementation DBJIllnessSecondView


/* 重写自定义视图UI方法 */
-(void)dbjSetupSubviews{
    [super dbjSetupSubviews];
    NSString *docuPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *fliePth= [docuPath stringByAppendingPathComponent:@"ill.nb"];
    self.illList=[NSKeyedUnarchiver unarchiveObjectWithFile:fliePth];
    if(self==nil){
        self.illList=[NSMutableArray array];
    }
    //添加中间tableView
   UITableView *midTabView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self addSubview:midTabView];
//    midTabView.backgroundColor=[UIColor blueColor];
    //记录属性
    self.tabView=midTabView;
    midTabView.delegate=self;
    midTabView.dataSource=self;
    midTabView.rowHeight=170;
    //注册cell
    [midTabView registerNib:[UINib nibWithNibName:@"DBJSecondTabCell" bundle:nil] forCellReuseIdentifier:@"secondCell"];
    
    //添加下部视图
    UIView *bottomView=[[UIView alloc]init];
    [self addSubview:bottomView];
    UIButton *addCaseBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    [addCaseBtn setImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    [addCaseBtn setTitle:@"   添加病历" forState:UIControlStateNormal];
    [addCaseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self addSubview:addCaseBtn];
    //给addbtn添加点击事件
    [addCaseBtn addTarget:self action:@selector(clickAddCaseBtn:) forControlEvents:UIControlEventTouchUpInside];
    //239	239	244
    bottomView.backgroundColor = [UIColor whiteColor];
    
    /* 设置约束 */
   //下部视图约束
   [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.bottom.right.equalTo(self);
       make.height.mas_equalTo(40);
   }];
    //设置添加病历button约束
    [addCaseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(-3);
    }];
    
    //设置中部tabView的约束
    [midTabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    

   
}

/* addbtn点击事件 */
-(void)clickAddCaseBtn:(UIButton *)sender{
    
    if([self.delegate respondsToSelector:@selector(IllSecondViewWithView:)]){
        
        [self.delegate IllSecondViewWithView:self];
    }
}

-(void)setIllList:(NSArray *)illList{
    _illList=illList;
    [self.tabView reloadData];
}
#pragma mark -实现数据源方法
/* 实现midTabVIew数据源方法 */
//返回单元格个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.illList.count;
    
}
//返回单元格
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DBJSecondTabCell *cell=[tableView dequeueReusableCellWithIdentifier:@"secondCell" forIndexPath:indexPath];
    DBJSecondTabModel *model=self.illList[indexPath.row];
    NSLog(@"%@",model);
    
    cell.model=model;
    
    return cell;
}

#pragma mark -实现代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
      if([self.delegate respondsToSelector:@selector(cellpushVCWithModel: andRow:)]){
          [self.delegate cellpushVCWithModel:self.illList[indexPath.row] andRow:indexPath.row];
      }
}

/** 设置分组style的头间隔 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

@end
